<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 优惠卷记录管理 
 * @author Mk
 *
 */
class Coupon_info extends Admin_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model(array("Coupons_model","Coupon_info_model","Users_model"));
	}
	public function index($page_no = 1){
		$page_no = max(intval($page_no),1);
		$where = '';
		$orderby = $keyword= "";
		if (isset($_GET['dosubmit'])) {
			$keyword =isset($_GET['keyword'])?safe_replace(trim($_GET['keyword'])):'';
			if($keyword!="") $where .= " and concat(c.coupon_name,u.nickName) like '%{$keyword}%'";
		
		}
		//echo $where;die;
		$data_list = $this->Coupon_info_model->listinfos($where,'cin.userid,cin.id,cin.state,cin.creatime,cin.usetime,cin.act_code,c.coupon_name,u.nickName',$orderby,$page_no,$this->Coupon_info_model->page_size,'',$this->Coupons_model->page_size,page_list_url('adminpanel/Coupon_info/index',true));
		$this->view('index',array('data_list'=>$data_list,'pages'=>$this->Coupon_info_model->pages,'keyword'=>$keyword,'require_js'=>true));
	}
	/**
	 * 指定用户
	 * @param int $id
	 */
	public function edit($id=0){
		$id = intval($id);
		$page_no = 1;
		$where = '';
		$orderby = $keyword= "";
		$data_list=array();
		if (isset($_GET['dosubmit'])) {
			$keyword =isset($_GET['keyword'])?safe_replace(trim($_GET['keyword'])):'';
			if($keyword!="") $where_arr[] = "concat(nickName) like '%{$keyword}%'";
			$where = implode(" and ",$where_arr);
			$data_list = $this->Users_model->listinfo($where,'*',$orderby , $page_no, $this->Users_model->page_size,'',$this->Users_model->page_size,page_list_url('adminpanel/Coupon_info/edit',true));
		}
		$this->view('edit',array('id'=>$id,"data_list"=>$data_list,'pages'=>"",'keyword'=>$keyword,'require_js'=>true));
	}
	/**
	 * 赠送
	 */
	public function appoint($id,$userId){
		if(empty($id)||empty($userId)){
			echo '<script type="text/javascript">alert("非法操作");location.href="'.SITE_URL.'/adminpanel/Coupon_info/index";</script>';
		}
		$flag = $this->Coupon_info_model->update(array("userid"=>$userId),array("id"=>$id));
		//show_error();
		if($flag){
			echo '<script type="text/javascript">alert("赠送成功");location.href="'.SITE_URL.'/adminpanel/Coupon_info/index";</script>';
		}else{
			echo '<script type="text/javascript">alert("赠送失败");location.href="'.SITE_URL.'/adminpanel/Coupon_info/index";</script>';
		}
	}
}
